package com.errol.batchprocessor.batchprocessor.bo;

import com.renjia.service.generic.middleware.batchprocessor.payload.BatchCounter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池和任务状态
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/10
 * @since v5.0.6
 **/
@Getter
@ApiModel
public class ThreadPoolAndMissionStatusBO extends ThreadPoolStatusBO {

    @ApiModelProperty("线程池中任务状态")
    private final List<MissionStatus> missionStatusList;

    public ThreadPoolAndMissionStatusBO(ThreadPoolExecutor executor, ConcurrentMap<String, BatchCounter> batchCounterMap) {
        super(executor);
        missionStatusList = new ArrayList<>();
        batchCounterMap.forEach((k, v) -> {
            missionStatusList.add(new MissionStatus(k, v));
        });
    }

    @Getter
    @ApiModel
    public static class MissionStatus {

        @ApiModelProperty("人员导入批次号")
        private final String batchNo;
        @ApiModelProperty("批次已启动的分片数")
        private final int startedPieces;
        @ApiModelProperty("批次未执行完的分片数")
        private final int undonePieces;
        @ApiModelProperty("批次是否已启动：null-未启动完成、true-启动成功、false-启动失败")
        private final Boolean batchStarted;
        @ApiModelProperty("是否存在执行失败的分片：false-无失败分片、true-存在失败分片")
        private final Boolean piecesFailed;

        public MissionStatus(String batchNo, BatchCounter batchCounter) {
            this.batchNo = batchNo;
            this.startedPieces = batchCounter.getStartedPieces().get();
            this.undonePieces = batchCounter.getUndonePieces().get();
            this.batchStarted = batchCounter.getBatchStarted();
            this.piecesFailed = batchCounter.getPiecesFailed();
        }
    }
}
